
<?php
	
	include_once "include/constants.php";
	
    if (!defined('IS_AJAX')) {
        define('IS_AJAX', isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
    }
	

	$userId = $session->userinfo["id"];
	try {
        $conn = new PDO('mysql:host='.DB_SERVER.';dbname='.DB_NAME, DB_USER, DB_PASS);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);    
       
        $stmt = $conn->prepare("select userid, u.movieid, u.rating, m.imdbid from user_ratings u inner join movies m on (m.movieid = u.movieid)
								where u.userid = " . $userId . " order by id desc;");
        $stmt->execute();
		    
        $imdbIds = array();
		$ratings = array();
		$ratingCount = 0;
		$ratingSum = 0;
        while (($rating = $stmt->fetch())) {
			if ($ratingCount < 5) {
				$imdbIds[$rating["movieid"]] = $rating["imdbid"];
				$ratings[$rating["movieid"]] = $rating["rating"];
			}
			$ratingCount++;
			$ratingSum += $rating["rating"];
        }
		
		if ($ratingCount != 0) {
			$ratingAverage = $ratingSum / $ratingCount;
		}
		else $ratingAverage = 0;
		
		$dLikes = array();
		$dDislikes = array();
		$aLikes = array();
		$aDislikes = array();
		$wLikes = array();
		$wDislikes = array();
		$gLikes = array();
		$gDislikes = array();
		
		$likes = $conn->prepare("select t.id, t.type, t.entity, t.rank, a.name as aName, d.name as dName, 
								w.name as wName 
								from thumbs t 
								left join actors a on (t.entity = a.actorid) 
								left join directors d on (t.entity = d.directorid) 
								left join writers w on (t.entity = w.writerid) where userid=" . $userId . " order by t.id desc;");
        $likes->execute();
		
		while ($like = $likes->fetch()) {
			if ($like["type"] == 0) {
				if($like["rank"] == 0) {
					$gDislikes[] = $genreId[$like["entity"]];
				}
				else if ($like["rank"] == 1) {
					$gLikes[] = $genreId[$like["entity"]];
				}
			}
			else if ($like["type"] == 1) {
				if($like["rank"] == 0) {
					$dDislikes[] = parse_name($like["dName"]);
				}
				else if ($like["rank"] == 1) {
					$dLikes[] = parse_name($like["dName"]);
				}
			}
			else if ($like["type"] == 2) {
				if($like["rank"] == 0) {
					$wDislikes[] = parse_name($like["wName"]);
				}
				else if ($like["rank"] == 1) {
					$wLikes[] = parse_name($like["wName"]);
				}
			}
			else if ($like["type"] == 3) {
				if($like["rank"] == 0) {
					$aDislikes[] = parse_name($like["aName"]);
				}
				else if ($like["rank"] == 1) {
					$aLikes[] = parse_name($like["aName"]);
				}
			}
		}
		
		$likesCounter = 0;
		$dislikesCounter = 0;
		if (count($aLikes) == 0) {
			$aLikes[0] = "No likes given";
		}
		else {
			$likesCounter += count($aLikes);
		}
		if (count($dLikes) == 0) {
			$dLikes[0] = "No likes given";
		}
		else {
			$likesCounter += count($dLikes);
		}
		if (count($wLikes) == 0) {
			$wLikes[0] = "No likes given";
		}
		else {
			$likesCounter += count($wLikes);
		}
		if (count($gLikes) == 0) {
			$gLikes[0] = "No likes given";
		}
		else {
			$likesCounter += count($gLikes);
		}
		if (count($aDislikes) == 0) {
			$aDislikes[0] = "No dislikes given";
		}
		else {
			$dislikesCounter += count($aDislikes);
		}
		if (count($dDislikes) == 0) {
			$dDislikes[0] = "No dislikes given";
		}
		else {
			$dislikesCounter += count($dDislikes);
		}
		if (count($wDislikes) == 0) {
			$wDislikes[0] = "No dislikes given";
		}
		else {
			$dislikesCounter += count($wDislikes);
		}
		if (count($gDislikes) == 0) {
			$gDislikes[0] = "No dislikes given";
		}
		else {
			$dislikesCounter += count($gDislikes);
		}
		
	
		
		//print_r($imdbIds);
		//print_r($queryResult);
                
        $result = array();
       
        
    } catch(PDOException $e) {
        echo 'ERROR: ' . $e->getMessage();
    }
?>

	<div>
		<table class="table">
			<div class="span4"><tr><td class="field span2">Total movies rated:</td><td class="span2"><?php echo $ratingCount; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Average movie rating:</td><td class="span2"> <?php echo round($ratingAverage, 1); ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Total likes:</td><td class="span2"> <?php echo $likesCounter; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Total dislikes:</td><td class="span2"> <?php echo $dislikesCounter; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Latest actor like:</td><td class="span2"> <?php echo $aLikes[0]; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Latest actor dislike:</td><td class="span2"> <?php echo $aDislikes[0]; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Latest director like:</td><td class="span2"> <?php echo $dLikes[0]; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Latest director dislike:</td><td class="span2"> <?php echo $dDislikes[0]; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Latest writer like:</td><td class="span2"> <?php echo $wLikes[0]; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Latest writer dislike:</td><td class="span2"> <?php echo $wDislikes[0]; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Latest genre like:</td><td class="span2"> <?php echo $gLikes[0]; ?></td></tr></div>
			<div class="span4"><tr><td class="field span2">Latest genre dislike:</td><td class="span2"> <?php echo $gDislikes[0]; ?></td></tr></div>
		</table>
		<?php if ($ratingCount > 0) : ?>
			<div>
				<h4>Last movies rated:</h4>
				<?php foreach ($imdbIds as $imdbid) :?> 
					<img src="img/posters/<?php echo $imdbid; ?>_small.jpg"></img>
				<?php endforeach ?>
			</div>
			
		<?php endif ?>
	</div>